﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PH.Model.Achievements;
using PH.Model.Standards;
using PH.Data.Standards;
namespace PH.Data.Statistics
{
    public class ScoreCompute
    {
        //public static void HAW(DBCentre db, MarkModel model)
        //{
        //    if (model.Achievement2 == null) throw new Utils.PhException("没有体重数据");

        //    IQueryable<EHAW> q = db.DataContext.EHAWs.Where(p => p.GradeCode == model.GradeCode && p.Sex == model.Sex);
        //    q = q.Where(p => p.HeightMin <= model.Achievement1 && p.HeightMax >= model.Achievement1);
        //    q = q.Where(p => p.WeightMin <= model.Achievement2 && p.WeightMax >= model.Achievement2);
        //    var e = q.FirstOrDefault();
        //    if (e != null)
        //    {
        //        model.Score = e.Score;
        //        model.Description = e.Description;
        //    }
        //    else
        //        throw new Utils.PhException("身高体重超出国家标准范围");
        //}
        /// <summary>
        /// 计算速度类项目的分数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="model"></param>
        /// <param name="sub"></param>
        public static void Speed(DBCentre db, MarkModel model)
        {
            //IQueryable<Evaluation> q = db.DataContext.Evaluations.Where(p =>p.SubjectID==model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex);
            //var lst = q.Where(p => !p.Additional).OrderByDescending(p => p.Score).ToList();
            var lst = EvaluationStandardCommand.List.FindAll(p => p.SubjectID == model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex).OrderByDescending(p=>p.Score).ToList();
            //var lstAdd = q.Where(p => p.Additional).OrderByDescending(p => p.Score).ToList();
            var lstAdd = EvaluationStandardCommand.AdditionalList.FindAll(p => p.SubjectID == model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex).OrderByDescending(p => p.Score).ToList();

            var n = lst.Count();
            if (lst != null && n > 0)
            {
                if (!model.AutoPass)
                {
                    var getScore = false;
                    foreach (var it in lst)
                    {
                        if (model.Achievement <= it.Mark)
                        {
                            model.Score = it.Score;
                            model.Description = it.Description;
                            getScore = true;
                            break;
                        }
                    }

                    #region 如果成绩超过标准最低线
                    if (!getScore)
                    {
                        model.Score = lst[n - 1].Score;
                        model.Description = lst[n - 1].Description;
                    }
                    #endregion

                    #region 计算附加分
                    if (model.Achievement < lst[0].Mark && lstAdd.Count > 0)
                    {
                        var addMark = lst[0].Mark - model.Achievement;
                        foreach (var it in lstAdd)
                        {
                            if (addMark >= it.Mark)
                            {
                                model.AdditionalScore = it.Score;
                                break;
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    var it = lst.Find(p => p.Score == 60);
                    if (it != null)
                    {
                        model.Score = it.Score;
                        model.Achievement = it.Mark;
                        model.Description = it.Description;
                    }
                }
            }
            else
                throw new Utils.PhException(string.Format("{0} 没有国家标准数据。", model.Subject != null ? model.Subject.SubjectName : ""));
        }
        /// <summary>
        /// 计算速度类项目的分数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="model"></param>
        /// <param name="sub"></param>
        public static void Distance(DBCentre db, MarkModel model)
        {
            //IQueryable<Evaluation> q = db.DataContext.Evaluations.Where(p => p.SubjectID == model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex);
            //var lst = q.Where(p => !p.Additional).OrderByDescending(p => p.Score).ToList();
            var lst = EvaluationStandardCommand.List.FindAll(p => p.SubjectID == model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex).OrderByDescending(p => p.Score).ToList();

            //var lstAdd = q.Where(p => p.Additional).OrderByDescending(p => p.Score).ToList();
            var lstAdd = EvaluationStandardCommand.AdditionalList.FindAll(p => p.SubjectID == model.SubjectID && p.GradeCode == model.GradeCode && p.Sex == model.Sex).OrderByDescending(p => p.Score).ToList();
            var n = lst.Count();
            if (lst != null && n > 0)
            {
                if (!model.AutoPass)
                {
                    var getScore = false;
                    foreach (var it in lst)
                    {
                        if (model.Achievement >= it.Mark)
                        {
                            model.Score = it.Score;
                            model.Description = it.Description;
                            getScore = true;
                            break;
                        }
                    }

                    #region 如果成绩超过标准最低线
                    if (!getScore)
                    {
                        model.Score = lst[n - 1].Score;
                        model.Description = lst[n - 1].Description;
                    }
                    #endregion

                    #region 计算附加分
                    if (model.Achievement > lst[0].Mark && lstAdd.Count > 0)
                    {
                        var addMark = model.Achievement - lst[0].Mark;
                        foreach (var it in lstAdd)
                        {
                            if (addMark >= it.Mark)
                            {
                                model.AdditionalScore = it.Score;
                                break;
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    var it = lst.Find(p => p.Score == 60);
                    if (it != null)
                    {
                        model.Score = it.Score;
                        model.Achievement = it.Mark;
                        model.Description = it.Description;
                    }
                }
            }
            else
                throw new Utils.PhException(string.Format("{0} 没有国家标准数据。", model.Subject != null ? model.Subject.SubjectName : ""));
        }
        /// <summary>
        /// 计算体重和身高比率的值（BMI）
        /// </summary>
        /// <param name="db"></param>
        /// <param name="model"></param>
        public static void BMI(DBCentre db, MarkModel model)
        {
            model.Achievement = Math.Round(model.Achievement, 1);
            //IQueryable<EBMI> q = db.DataContext.EBMIs.Where(p => p.GradeCode == model.GradeCode && p.Sex == model.Sex);
            //q = q.Where(p => p.Min <=model.Achievement && p.Max >= model.Achievement);
            //var e = q.FirstOrDefault();
            var e = EBMICommand.List.FirstOrDefault(p => p.GradeCode == model.GradeCode && p.Sex == model.Sex && p.Min <= model.Achievement && p.Max >= model.Achievement);
            if (e != null)
            {
                //var em = Convertor.ConvertTo<EBMIModel, EBMI>(e);
                model.Score = e.Score;
                model.Description = e.Level;
            }
            else
                throw new Utils.PhException("身高体重超出国家标准范围");
        }
    }
}
